﻿#include "DAWorkFlowOperateWidget.h"
// qt
#include <QImage>
#include <QDebug>
// moc
#include "ui_DAWorkFlowOperateWidget.h"
// workflow
#include "DAWorkFlowGraphicsView.h"
#include "DAWorkFlowGraphicsScene.h"
#include "DAGraphicsResizeablePixmapItem.h"
// api
#include "DAAppCore.h"
#include "DAAppRibbonArea.h"
#include "DAAppCommand.h"
#include "DAAppUI.h"
#include <QUndoStack>
//
#include "DACommandGraphicsTextItemProperty.h"
//===================================================
// using DA namespace -- 禁止在头文件using！！
//===================================================

using namespace DA;

//===================================================
// DAWorkFlowOperateWidget
//===================================================
DAWorkFlowOperateWidget::DAWorkFlowOperateWidget(QWidget* parent) : QWidget(parent), ui(new Ui::DAWorkFlowOperateWidget)
{
    ui->setupUi(this);
}

DAWorkFlowOperateWidget::~DAWorkFlowOperateWidget()
{
    delete ui;
}

void DAWorkFlowOperateWidget::setProject(DAProject* project)
{
    ui->workflowGraphicsView->setProject(project);
}

void DAWorkFlowOperateWidget::setNodeFactory(DADataWorkFlow* factory)
{
    ui->workflowGraphicsView->setWorkFlow(factory);
}

/**
 * @brief 获取工作流操作视图
 * @return
 */
DAWorkFlowGraphicsView* DAWorkFlowOperateWidget::getWorkFlowGraphicsView() const
{
    return ui->workflowGraphicsView;
}
/**
 * @brief 获取GraphicsScene
 * @return
 */
DAWorkFlowGraphicsScene* DAWorkFlowOperateWidget::getWorkFlowGraphicsScene() const
{
    return qobject_cast< DAWorkFlowGraphicsScene* >(getWorkFlowGraphicsView()->scene());
}

void DAWorkFlowOperateWidget::setUndoStackActive()
{
    getWorkFlowGraphicsView()->setUndoStackActive();
}

/**
 * @brief 设置显示grid
 * @param on
 */
void DAWorkFlowOperateWidget::setEnableShowGrid(bool on)
{
    DAWorkFlowGraphicsScene* scene = getWorkFlowGraphicsScene();
    scene->showGridLine(on);
    scene->update();
}

/**
 * @brief 获取undostack
 * @return
 */
QUndoStack* DAWorkFlowOperateWidget::getUndoStack()
{
    return getWorkFlowGraphicsView()->getUndoStack();
}

void DAWorkFlowOperateWidget::addBackgroundPixmap(const QString& pixmapPath)
{
    QImage img(pixmapPath);
    QPixmap px;
    px.convertFromImage(img);
    DAGraphicsResizeablePixmapItem* item = getWorkFlowGraphicsScene()->setBackgroundPixmap(px);
    item->setSelectable(true);
    item->setMoveable(true);
    // connect(item, &DAGraphicsResizeablePixmapItem::itemPosChange, this, &DAWorkFlowOperateWidget::onItemPosChange);
    DAAppRibbonArea* ribbon = qobject_cast< DAAppRibbonArea* >(DAAppCore::getInstance().getUiInterface()->ribbonArea());
    if (ribbon) {
        ribbon->updateActionLockBackgroundPixmapCheckStatue(false);
    }
}

void DAWorkFlowOperateWidget::setBackgroundPixmapLock(bool on)
{
    DAGraphicsResizeablePixmapItem* item = getWorkFlowGraphicsScene()->getBackgroundPixmapItem();
    if (nullptr == item) {
        return;
    }
    item->setSelectable(!on);
    item->setMoveable(!on);
}

void DAWorkFlowOperateWidget::setSelectTextToBold(bool on)
{
    auto secen = getWorkFlowGraphicsScene();

    secen->setTextBold(on);
    QList< DAStandardGraphicsTextItem* > items = getSelectTextItems();
    QList< QFont > newFonts;
    for (DAStandardGraphicsTextItem* item : qAsConst(items)) {
        QFont font = item->font();
        font.setBold(on);
        newFonts.append(font);
    }
    DA::DACommandGraphicsTextItemsChangeFont* cmd = new DA::DACommandGraphicsTextItemsChangeFont(items, newFonts);
    secen->push(cmd);
}

void DAWorkFlowOperateWidget::setSelectTextToItalic(bool on)
{
    auto secen = getWorkFlowGraphicsScene();
    secen->setTextItalic(on);
    QList< DAStandardGraphicsTextItem* > items = getSelectTextItems();
    QList< QFont > newFonts;
    for (DAStandardGraphicsTextItem* item : qAsConst(items)) {
        QFont font = item->font();
        font.setItalic(on);
        newFonts.append(font);
    }
    DA::DACommandGraphicsTextItemsChangeFont* cmd = new DA::DACommandGraphicsTextItemsChangeFont(items, newFonts);
    secen->push(cmd);
}

void DAWorkFlowOperateWidget::setSelectTextColor(const QColor& color)
{
    auto secen = getWorkFlowGraphicsScene();
    secen->setTextColor(color);
    QList< DAStandardGraphicsTextItem* > items = getSelectTextItems();
    QList< QColor > colors;
    for (DAStandardGraphicsTextItem* item : qAsConst(items)) {
        colors.append(color);
    }
    DA::DACommandGraphicsTextItemsChangeColor* cmd = new DA::DACommandGraphicsTextItemsChangeColor(items, colors);
    secen->push(cmd);
}

void DAWorkFlowOperateWidget::setSelectTextFamily(const QString& family)
{
    auto secen = getWorkFlowGraphicsScene();
    secen->setTextFamily(family);
    QList< DAStandardGraphicsTextItem* > items = getSelectTextItems();
    QList< QFont > newFonts;
    for (DAStandardGraphicsTextItem* item : qAsConst(items)) {
        QFont font = item->font();
        font.setFamily(family);
        newFonts.append(font);
    }
    DA::DACommandGraphicsTextItemsChangeFont* cmd = new DA::DACommandGraphicsTextItemsChangeFont(items, newFonts);
    secen->push(cmd);
}

void DAWorkFlowOperateWidget::setTextSize(const int size)
{
    auto secen = getWorkFlowGraphicsScene();
    secen->setTextSize(size);
    QList< DAStandardGraphicsTextItem* > items = getSelectTextItems();
    QList< QFont > newFonts;
    for (DAStandardGraphicsTextItem* item : qAsConst(items)) {
        QFont font = item->font();
        font.setPointSize(size);
        newFonts.append(font);
    }
    DA::DACommandGraphicsTextItemsChangeFont* cmd = new DA::DACommandGraphicsTextItemsChangeFont(items, newFonts);
    secen->push(cmd);
}

QList< DAStandardGraphicsTextItem* > DAWorkFlowOperateWidget::getSelectTextItems()
{
    QList< DAStandardGraphicsTextItem* > res;
    auto secen                  = getWorkFlowGraphicsScene();
    QList< QGraphicsItem* > its = secen->selectedItems();
    if (its.size() == 0) {
        return res;
    }
    for (QGraphicsItem* item : qAsConst(its)) {
        if (DAStandardGraphicsTextItem* textItem = dynamic_cast< DAStandardGraphicsTextItem* >(item)) {
            res.append(textItem);
        }
    }
    return res;
}

/**
 * @brief 运行工作流
 */
void DAWorkFlowOperateWidget::runWorkFlow()
{
    DAWorkFlow* wf = ui->workflowGraphicsView->getWorkflow();
    if (nullptr == wf) {
        qCritical() << tr("no workflow set");
    }
    wf->exec();
}
